import java.io.File;
import java.util.Scanner;

public class Tree {
	
	private String key;
	private String filePath;    
	protected static String objectPath;
	private StringBuffer value;
	
	public Tree() {}
	public Tree(String P1, String P2){
		filePath = P1;
		objectPath = P2;
		gen_tree();
		
	}
	
	public void gen_tree() {
		File file1 = new File( filePath );
		value = new StringBuffer();
		try {
			if( file1.isFile()) {
				Blob b = new Blob( filePath, objectPath );
				value.append( b.getValue() );
			}
			else if (file1.isDirectory()) {
				File[] file2 = file1.listFiles();
				
				for(int i=0; i<file2.length; i++) {
					Tree_content g = new Tree_content( file2[i].getPath());
					if( i!=0 ) 
						value.append("\n");
					value.append( g.output() );
					
					
				}
				
				Gen_hash t = new Gen_hash();
				key = t.hashString( value.toString() );
				
				File tree = new File( objectPath + "\\" + key );
				
				if ( !tree.exists() ) {       
					tree.createNewFile();
					PrintWriter output = new PrintWriter( tree );
					output.write( value.toString() );
					output.close();
				}
			}	
		}
		catch (IOException e) {	
			e.printStackTrace();
		}  		
	}
	
	
	public String getFilePath() {
		return filePath;
	}
	
	public String getObjectPath() {
		return objectPath;
	}
	
	public String getKey() {
		return key;
	}
	
	public  String getValue ( ) throws FileNotFoundException {
		 GetValue t = new GetValue();
		 return t.getValue( objectPath +"//" + key );
	 }
}
    
   